package com.xu.mb3.mbtools;

import com.sun.corba.se.pept.transport.ContactInfo;
import lombok.Data;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuguan
 * @since 2024/11/15
 */
public class ReflectorSample {

    public static void reflectorSample() {
        List<Address> addressList = new ArrayList<>();
        final Address address = new Address();
        address.setProvince("浙江省");
        address.setCity("杭州市");
        address.setDistinct("西湖区");
        addressList.add(address);

        final CertInfo certInfo = new CertInfo();
        certInfo.setCertType("身份证");
        certInfo.setCertNo("100010199901013433");

        final User user = new User();
        user.setName("fish");
        user.setAddressList(addressList);
        user.setCertInfo(certInfo);

        final MetaObject metaObject = SystemMetaObject.forObject(user);

        final Object name = metaObject.getValue("name");
        System.out.println("name = " + name);

        final Object certNo = metaObject.getValue("certInfo.certNo");
        System.out.println("certNo = " + certNo);

        final Object province = metaObject.getValue("addressList[0].province");
        System.out.println("province = " + province);
    }

    @Data
    private static class User {
        private String name;
        private CertInfo certInfo;
        private List<Address> addressList;
    }

    @Data
    private static class Address {
        private String province;
        private String city;
        private String distinct;
    }

    @Data
    private static class CertInfo {
        private String certType;
        private String certNo;
    }
    public static void main(String[] args) {
        reflectorSample();
    }
}